package com.why.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.vo.CommentCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.why.common.utils.PageUtils;
import com.why.common.utils.Query;

import com.why.dao.ProductCommentsDao;
import com.why.entity.ProductCommentsEntity;
import com.why.service.ProductCommentsService;


@Service("productCommentsService")
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsDao, ProductCommentsEntity> implements ProductCommentsService {
    @Autowired
    ProductCommentsDao productCommentsDao;

    @Override
    public IPage queryPage(Map<String, Object> params) {
        QueryWrapper<ProductCommentsEntity> wrapper = new QueryWrapper<>();
        Long productId = Long.valueOf(String.valueOf(params.get("productId")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
        int limit = Integer.parseInt(String.valueOf(params.get("limit")));
        IPage<ProductCommentsEntity> page = new Page<ProductCommentsEntity>(pageNum, limit);
        List list = this.productCommentsDao.selectCommontsByProductId(page, productId);
        page.setRecords(list);

        return page;
    }

    @Override
    public CommentCount queryCount(Long productId) {

        Integer total = this.productCommentsDao.selectCommentCount(productId, null);
        Integer bad = this.productCommentsDao.selectCommentCount(productId, -1);

        Integer good = this.productCommentsDao.selectCommentCount(productId, 1);
        Integer mid = this.productCommentsDao.selectCommentCount(productId, 0);

        Double percent = null;
        if (total != 0) {
            percent = Double.parseDouble(good + "") / Double.parseDouble(total + "");
        }
        return new CommentCount(total, good, mid, bad, percent);

    }

}